from django import forms

from saas_main import models
from django.core.validators import RegexValidator


class RegisterForms(forms.ModelForm):
    password = forms.CharField(label="密码", widget=forms.PasswordInput)
    confirm_password = forms.CharField(label="确认密码", widget=forms.PasswordInput)
    code = forms.CharField(label="验证码")

    class Meta:
        model = models.User
        fields = ["username", "password", "confirm_password", "realname", "email", "mobile", "code"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # name 就是字段名字 field就是生成的input的标签对象
        for name, field in self.fields.items():
            if isinstance(field, forms.BooleanField):
                continue
            if name == 'username':
                field.label = "用户名"
            if name == 'email':
                field.label = "邮箱"
            # 这里会给每个标签对象属性重新赋值，不会保留原有属性，所以如果想保留原来属性需要进行判断(可以考虑update逻辑)
            if field.widget.attrs:
                field.widget.attrs['class'] = "form-control"
            else:
                field.widget.attrs = {"class": "form-control"}
            field.widget.attrs["placeholder"] = f"请输入{field.label}"


class LoginForms(forms.Form):
    account = forms.CharField(label='账号')
    password = forms.CharField(label="密码", widget=forms.PasswordInput)
    code = forms.CharField(label='验证码')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # name 就是字段名字 field就是生成的input的标签对象
        for name, field in self.fields.items():
            # 这里会给每个标签对象属性重新赋值，不会保留原有属性，所以如果想保留原来属性需要进行判断(可以考虑update逻辑)
            if field.widget.attrs:
                field.widget.attrs['class'] = "form-control"
            else:
                field.widget.attrs = {"class": "form-control"}
            if name == 'account':
                field.widget.attrs["placeholder"] = f"请输入用户名/手机号/邮箱"
            else:
                field.widget.attrs["placeholder"] = f"请输入{field.label}"